<template>
  <div class="login_body">
    <br><br>
    <input type="text" v-model="loginForm.username" placeholder="用户名"/>
    <br><br>
    <input type="password" v-model="loginForm.password" placeholder="密码"/>
    <br><br>
    <button v-on:click="login">登录</button>
  </div>
</template>
<style>
  .login_body{
    width:14vw;
    height: 25vh;
    /*background: #b7cbcc;*/
    position: relative;
    left: 42vw;
    top: 2vh;
  }
  input{
    width: 80%;
    height: 15%;
  }
  button{
    background-color: slategrey;
    color: aliceblue;
    border:0;
    font-size: 16px;
    /*width: 12vw;*/
    /*height: 3vh;*/
    width: 84%;
    height: 15%;
  }
  button:hover{
    background-color: aliceblue;
    color: slategrey;
  }
</style>
<script>
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      responseResult: []
    }
  },
  methods: {
    login () { // 登录
      var _this = this
      console.log(this.$store.state)
      if (!this.loginForm.username) {
        alert('用户名不能为空')
        return null
      }
      if (!this.loginForm.password) {
        alert('请输入密码')
        return null
      }
      this.$axios
        .post('/login', {
          username: this.loginForm.username,
          password: this.loginForm.password
        })
        .then(successResponse => {
          if (successResponse.data.code === 200) {
            _this.$store.commit('login', _this.loginForm)
            var path = this.$route.query.redirect
            this.$router.replace({path: path === '/' || path === undefined ? '/index' : path})
            return true
          } else {
            alert('用户名或密码错误')
          }
        })
        .catch(failResponse => {
        })
    }
  }
}
</script>

<style scoped>

</style>
